<template>
	<view :class="themeStyle">
		<view class="container" :class="isBottom?'bottom':''">
			<view class="top_siteClass" @click="popStaFun" v-show="jdshop">{{locationObj.full_address}}</view>
				
			
			<block v-if="isBottom">
				<view class="cart-wrap" v-for="(siteItem, siteIndex) in cartData" :key="siteIndex">
					<view class="cart-header">
						<view class="iconfont" :class="siteItem.checked ? 'iconyuan_checked ns-text-color' : 'iconyuan_checkbox'" @click="siteAllElection(!siteItem.checked, siteIndex)"></view>
						<view class="iconfont icondianpu"></view>
						<view class="shop-info">
							<text>{{ siteItem.siteName }}</text>
						</view>
						<view class="cart-operation" @click="edit(siteIndex)">{{ siteItem.edit ? $lang('complete') : $lang('edit') }}</view>
					</view>
					<block v-for="(item, cartIndex) in siteItem.cartList" :key="cartIndex">
						<view class="cart-goods">
							<view class="goods-wrap" :class="{ edit: siteItem.edit }">
								<view class="iconfont" :class="item.checked ? 'iconyuan_checked ns-text-color' : 'iconyuan_checkbox'" @click="singleElection(siteIndex, cartIndex, item.repertoryChecked)"></view>
								<navigator hover-class="none" class="goods-img" :url="'/pages/goods/detail/detail?sku_id=' + item.sku_id">
									<image :src="$util.img(item.sku_image, { size: 'mid' },item.source)" @error="imageError(siteIndex,cartIndex)" mode="aspectFill"></image>
								</navigator>
								<view class="goods-info">
									<navigator hover-class="none" :url="'/pages/goods/detail/detail?sku_id=' + item.sku_id" class="goods-name">{{ item.sku_name }}</navigator>
									<text class="sku" v-if=" item.max_buy > 0 ">限购{{item.max_buy}}件</text>
									<view class="goods-sub-section">
										<view class="goods-price-box">
											<view class="goods-price ns-text-color tag" v-show="item.repertorySta">
												<text class="tag">{{item.repertorySta}}</text>
											</view>
											<!-- <text class="goods-price ns-text-color tag">
												<text class="tag">{{ item.price_pattern_name }}</text>
											</text> -->
											<text class="goods-price ns-text-color" v-for="(val, index) in item.show_price_info" :key="index">
												<text class="unit">{{ val.unit }}</text> {{ val.discount_price }}
											</text>
										</view>
										<uni-number-box 
											:min="item.min_buy > 0 ? item.min_buy : 1" 
											:max="item.stock > item.max_buy && item.max_buy> 0 ? item.max_buy:item.stock"
											:value="item.num" 
											size="small" 
											:modifyFlag="modifyFlag"
											:source="item.source"
											@change="cartNumChange($event, { siteIndex, cartIndex })" 
										/>
									</view>
								</view>
							</view>
							<view class="item-del ns-bg-color" :class="{ show: siteItem.edit }" @click="deleteCart(siteIndex, cartIndex)">{{ $lang('del') }}</view>
						</view>
					</block>
				</view>

				<view class="cart-wrap" v-if="invalidGoods.length">
					<view class="cart-header">
						<view class="shop-info">
							<text class="ns-font-size-sm">失效商品{{ invalidGoods.length }}件</text>
						</view>
						<view class="cart-operation ns-text-color ns-font-size-sm" @click="clearInvalidGoods">清空失效商品</view>
					</view>
					<block v-for="(goodsItem, goodsIndex) in invalidGoods" :key="goodsIndex">
						<view class="cart-goods invalid-goods">
							<view class="invalid-mark">失效</view>
							<view class="goods-wrap">
								<view class="goods-img">
									<image :src="$util.img(goodsItem.sku_image, { size: 'mid' },goodsItem.source)" mode="aspectFill"></image>
								</view>
								<view class="goods-info">
									<view class="goods-name">{{ goodsItem.sku_name }}</view>
									<text class="sku"></text>
									<view class="goods-sub-section">
										<text class="goods-price ns-text-color">
											<text class="unit">{{ $lang('common.currencySymbol') }}</text>
											{{ goodsItem.discount_price }}
										</text>
									</view>
								</view>
							</view>
						</view>
					</block>
				</view>
			</block>
			<block v-else>
				<view class="cart-empty">
					<ns-empty :text="$lang('emptyTips')" v-if="token != ''" :fixed="!1"></ns-empty>
					<ns-empty :text="$lang('emptyTips')" :emptyBtn="emptyBtn1" v-else :fixed="!1" :urlParam="urlParam"></ns-empty>
				</view>
			</block>
			<nsGoodsRecommend ref="goodrecommend"></nsGoodsRecommend>
		</view>
		<view class="cart-bottom" v-if="cartData.length||invalidGoods.length">
			<view class="all-election" @click="allElection">
				<view class="iconfont" :class="checkAll ? 'iconyuan_checked ns-text-color' : 'iconyuan_checkbox'"></view>
				<text>{{ $lang('allElection') }}</text>
			</view>
			<view class="settlement-info">
				<text class="ns-text-color right">
					<view class="count-line-title top">
						<text class="total">合计:</text>
						<text v-if="totalBalance != 0"> 余额 {{ totalBalance }}</text>
					</view>
					<view class="count-line bottom">
						<text v-if="totalPoint != 0">积分 {{ totalPoint }}</text>
						<text v-if="totalRedPoint != 0">红积分 {{ totalRedPoint }}</text>
						<text>君政券 {{ totalJunzhengTicket }}</text>
					</view>
				</text>
			</view>
			<view class="operation-btn">
				<button type="primary" size="mini" @click="settlement(1)" v-if="totalCount != 0">{{ $lang('settlement') }}({{ totalCount }})</button>
				<button type="default" class="btn-color" size="mini" @click="settlement(2)" v-if="totalCount != 0 && is_store">代客下单({{ totalCount }})</button>
				<button type="primary" size="mini" @click="settlement(1)" disabled v-if="totalCount == 0">{{ $lang('settlement') }}({{ totalCount }})</button>
			</view>
		</view>
		<view class="blockView" v-if="cartData.length||invalidGoods.length">

		</view>
		<!-- 代客下单 -->
		<!-- Instead_array -->
		<uni-popup ref="Instead_popup" type="bottom" class="Instead_popup" :custom="true">
			<view class="Instead-title">
				请客户扫描二维码或者保存发送二维码
			</view>
			<view class="Instead-box">
				<image :src="$util.img(CodeImg)" mode=""></image>
			</view>
			<view class="Instead-footer">
				<button class="close-btn" type="default" @click="close_popup()">关闭</button>
				<button class="sue-btn" type="default" @click="save_img()">保存二维码</button>
			</view>
		</uni-popup>
		<!-- 加载动画 -->
		<loading-cover ref="loadingCover"></loading-cover>
		<!-- 底部tabBar -->
		<diy-bottom-nav></diy-bottom-nav>
		<!-- 返回顶部 -->
		<to-top v-if="showTop" @toTop="scrollToTopNative()"></to-top>
		
		
		
		<!-- 展示收货地址弹窗 -->
		<u-popup v-model="popTrueShow" mode="bottom"  height="80%" border-radius="20">
			<view class="lch_pop">
				<view class="lch_pop_tit">配送至</view>
				<view class="lch_pop_ListA">
					<view class="lch_pop_List_min" v-for="(item,index) in sizeConList" :key="index" :style="item.colorSta ? 'color: #007aff;' : (item.is_default == 1 ? 'color: #007aff;' : '')" @click="siteSelectClick(item,index)">{{item.full_address}}</view>
				</view>
				<view class="lch_pop_ListA_btn" @click="siteSelectBtn">选择其他收货地址</view>
			</view>
		</u-popup>
		
		<!-- 选择地址弹窗 -->
		<u-popup v-model="popShow" mode="bottom"  height="80%" border-radius="20">
			<view class="lch_pop">
				<view class="lch_pop_tit">配送至</view>
				<view class="lch_pop_conwrap">
					<view class="lch_pop_con" v-for="(item,index) in popCon" :key="index" :style="bottomSta == index ? 'border-bottom: 4rpx solid #007aff;':''" @click="popConBtn(item,index)">{{item.name}}</view>
					<view class="lch_pop_con" v-show="popCon.length < 4" :style="bottomSta == 4 ? 'border-bottom: 4rpx solid #007aff;':''" @click="popConBtn('',4)">请选择</view>
				</view>
				<view class="lch_pop_List">
					<view class="lch_pop_List_min" v-for="(item,index) in popList" :key="index" @click="popBtn(item.name,item.id)" :style="bottomSta < 4 && item.id == popCon[bottomSta].id ? 'color: #007aff':''">{{item.name}}</view>
				</view>
			</view>
		</u-popup>
		
		
	</view>
</template>

<script>
	import nsGoodsRecommend from '@/components/ns-goods-recommend/ns-goods-recommend.vue';
	import uniNumberBox from '@/components/uni-number-box/uni-number-box.vue';
	import diyBottomNav from '@/components/diy-bottom-nav/diy-bottom-nav.vue';
	import toTop from '@/components/toTop/toTop.vue';
	import scroll from '@/common/js/scroll-view.js';
	import config from '@/common/js/config.js';

	export default {
		components: {
			nsGoodsRecommend,
			uniNumberBox,
			diyBottomNav,
			toTop
		},
		mixins: [scroll],
		data() {
			return {
				token: '',
				newArr:[],
				jdshop:false,
				cartData: [], // 购物车
				checkAll: false,
				totalPrice: '0.00',
				totalBalance: 0.00,
				totalPoint: 0.00,
				totalRedPoint: 0.00,
				totalJunzhengTicket: 0.00,
				totalCount: 0,
				modifyFlag: false,
				isSub: false,
				invalidGoods: [], // 失效商品集合
				emptyBtn1: {
					text: "去登录",
					url: '/pages/login/login/login'
				},
				urlParam: '/pages/goods/cart/cart',
				CodeImg: '', //代客下单二维码
				is_store: false ,//是否为商家
				//默认收货地址
				defaultAddress:null,
				// 以下定位地址-----------------------
				siteSta: false,
				popTrueShow: false,
				sizeConList:[],
				popShow: false,
				popCon:[],
				popConSta: 0,
				popList:[],
				listAr:[],
				popIdA:'',
				popIdB:'',
				popIdC:'',
				popIdD:'',
				bottomSta: 4,
				vehicleModel:'',
				inventorySta: '',
				checkedStaCon: [],
				locationObj:{
					province_id: '110000',
					city_id: '110100',
					district_id: '110101',
					community_id: '2821',
					full_address: '北京-北京市-东城区-内环到三环里'
				},
				// 以上定位地址----------------------------
				initialCase: 1,
				addressId:0,
			};
		},
		onLoad() {
			// 拿地址信息
			this.getSiteListData();
			// 那城市列表
			this.popListFun();
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
			if (uni.getStorageSync('token')) {
				this.getCartData();
				this.check_store();
				this.getDefaultAddress();
			}
		},
		onReady() {
			if (!uni.getStorageSync('token')) {
				if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
			}
		},
		computed: {
			isBottom() {
				return this.cartData.length > 0 || this.invalidGoods.length > 0
			},

			//vueX页面主题
			themeStyle() {
				return 'theme-' + this.$store.state.index.themeStyle
			}
		},
		methods: {
			// 拿取收货地址列表
			getSiteListData() {
				let that = this;
				this.$api.sendRequest({
					url: '/api/memberaddress/page',
					data: {
						page: 1,
						page_size: 100
					},
					success: res => {
						if(res.data.list.length > 0){
							that.siteSta = true;
							that.sizeConList = res.data.list;
							
							for(let i=0; i<res.data.list.length; i++){
								if(res.data.list[i].is_default == 1){
									
									that.locationObj = res.data.list[i];
									// that.vehicleModel = res.data.list[i].full_address;
									
									
									// let siteId = {
									// 	province_id: res.data.list[i].province_id,
									// 	city_id: res.data.list[i].city_id,
									// 	district_id: res.data.list[i].district_id,
									// 	community_id: res.data.list[i].community_id
									// }
									
									// that.inventoryConFun(siteId);
								}
							}
						}else{
							this.locationConFun();
							this.siteSta = false;
						}
					},
					fail: res => {
			
					}
				});
			},
			getCartData() {
				this.$api.sendRequest({
					url: '/api/cart/lists',
					success: res => {
						if (res.code >= 0) {
							this.newArr=res.data;
							console.log("shuju1",this.newArr)
							if(this.newArr==''){
								this.jdshop=false
							}
							
							this.token = uni.getStorageSync('token');
							if (res.data.length) this.handleCartData(res.data);
							else this.cartData = [];
							if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
							
							for(let i=0;i<this.newArr.length;i++){
								if(this.newArr[i].source == 2){
									this.jdshop=true
									this.$forceUpdate()
									return false;
								}else{
									this.jdshop=false
								}
							}
						} else {
							this.token = '';
						}
						
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			// 选择收货地址按钮
			siteSelectBtn(){
				this.popTrueShow = false;
				this.popShow = true;
			},
			// 打开弹窗
			popStaFun(){
				if(this.siteSta){
					this.popTrueShow = true;
				}else{
					this.popShow = true;
				}
			},
			
			// 选择已有的收货地址
			siteSelectClick(val,index){
				console.log('asd',val.id)
				// console.log(val,'地址id');
			
				this.addressId=val.id
				this.locationObj = val;
				this.vehicleModel = val.full_address;
				
				this.popTrueShow = false;
				this.popShow = false;
				
				// let siteId = {
				// 	province_id: val.province_id,
				// 	city_id: val.city_id,
				// 	district_id: val.district_id,
				// 	community_id: val.community_id
				// }
				this.inventoryConFun();
				this.getAddress(val.id);
			},
			
			
			/**
			 * 获取购物车数据
			 */
			
			// 检查是否为商户
			check_store() {
				this.$api.sendRequest({
					url: '/api/member/isShop',
					success: res => {
						if (res.code >= 0) {
							if (res.data.is_shop == 1) {
								this.is_store = true
							}
						}
					}
				});
			},
			/**
			 * 处理购物车数据结构
			 */
			handleCartData(data) {
				this.invalidGoods = [];
				this.cartData = [];
				var temp = {};
				data.forEach((item, index) => {
					if (item.goods_state == 1 && item.verify_state == 1 && item.shop_status != 0) {
						item.checked = true;
						item.show_price_info = this.$util.getShowPriceInfo(item);
						if (temp['site_' + item.site_id] != undefined) {
							temp['site_' + item.site_id].cartList.push(item);
						} else {
							temp['site_' + item.site_id] = {
								siteId: item.site_id,
								siteName: item.site_name,
								edit: false,
								checked: true,
								cartList: [item]
							};
						}
					} else {
						this.invalidGoods.push(item);
					}
				});

				this.cartData = [];
				Object.keys(temp).forEach(key => {
					this.cartData.push(temp[key]);
				});
				this.calculationTotalPrice();
			},
			/**
			 * 单选
			 * @param {Object} index
			 */
			singleElection(siteIndex, index, sta) {
				// if(sta === 'ban'){
				// 	uni.showToast({
				// 		title:'该商品库存不足',
				// 		icon:'none'
				// 	})
				// 	return;
				// }
				this.cartData[siteIndex].cartList[index].checked = !this.cartData[siteIndex].cartList[index].checked;
				this.calculationTotalPrice();
			},
			/**
			 * 店铺全选
			 * @param {Object} checked
			 */
			siteAllElection(checked, index) {
				this.cartData[index].checked = checked;
				this.cartData[index].cartList.forEach(item => {
					item.checked = checked;
				});
				this.calculationTotalPrice();
			},
			/**
			 * 全选
			 */
			allElection(checked) {
				if (typeof checked == 'boolean') {
					this.checkAll = checked;
				} else {
					this.checkAll = !this.checkAll;
				}
				if (this.cartData.length) {
					this.cartData.forEach(siteItem => {
						siteItem.checked = this.checkAll;
						siteItem.cartList.forEach(item => {
							item.checked = this.checkAll;
						});
					});
				}
				this.calculationTotalPrice();
			},
			/**
			 * 计算购物车总价
			 */
			calculationTotalPrice(sta) {
				if (this.cartData.length) {
					let totalPrice = 0,
						totalBalance = 0,
						totalPoint = 0,
						totalRedPoint = 0,
						totalJunzhengTicket = 0,
						totalCount = 0,
						siteAllElectionCount = 0;

					this.cartData.forEach(siteItem => {
						let siteGoodsCount = 0;
						siteItem.cartList.forEach(item => {
							if (item.checked) {
								siteGoodsCount += 1;
								totalCount += 1;
								totalPrice += item.discount_price * item.num;
								totalBalance += item.discount_balance * item.num;
								totalPoint += item.discount_point * item.num;
								totalRedPoint += item.discount_red_point * item.num;
								totalJunzhengTicket += item.discount_junzheng_ticket * item.num;
							}
						});
						if (siteItem.cartList.length == siteGoodsCount) {
							siteItem.checked = true;
							siteAllElectionCount += 1;
						} else {
							siteItem.checked = false;
						}
					});
					this.totalPrice = totalPrice.toFixed(2);
					this.totalBalance = totalBalance.toFixed(2);
					this.totalRedPoint = totalRedPoint.toFixed(2);
					this.totalPoint = totalPoint.toFixed(2);
					this.totalJunzhengTicket = totalJunzhengTicket.toFixed(2);
					this.totalCount = totalCount;
					this.checkAll = this.cartData.length == siteAllElectionCount;
					
				} else {
					this.totalPrice = '0.00';
					this.totalCount = 0;
				}
				if(!sta){
					this.checkedStaFun();
				}
				this.modifyFlag = false;
			},
			/**
			 * 删除购物车
			 * @param {Object} siteIndex
			 * @param {Object} cartIndex
			 */
			deleteCart(siteIndex, cartIndex) {
				this.$api.sendRequest({
					url: '/api/cart/delete',
					data: {
						cart_id: this.cartData[siteIndex].cartList[cartIndex].cart_id
					},
					success: res => {
						if (res.code >= 0) {

							
							this.cartData[siteIndex].cartList.splice(cartIndex, 1);
							if (this.cartData[siteIndex].cartList.length == 0) this.cartData.splice(siteIndex, 1);
							this.calculationTotalPrice();
							this.getCartNumber();
							this.getCartData();
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
				
			},
			/**
			 * 变更购物车数量
			 * @param {Object} params
			 */
			cartNumChange(num, params) {
				// 防止数据渲染触发调用
				if(this.initialCase < 3){
					this.initialCase += 1;
					return;
				};
				
				if (num < 1) num = 1;
				this.modifyFlag = true;
				this.$api.sendRequest({
					url: '/api/cart/edit',
					data: {
						num,
						cart_id: this.cartData[params.siteIndex].cartList[params.cartIndex].cart_id,
						province_id:this.locationObj.province_id,
						city_id:this.locationObj.city_id,
						district_id:this.locationObj.district_id,
						community_id:this.locationObj.community_id,
					},
					success: res => {
						if (res.code >= 0) {
							this.cartData[params.siteIndex].cartList[params.cartIndex].num = num;
							let onlyObj = {
								source: this.cartData[params.siteIndex].cartList[params.cartIndex].source,
								source_sku_id: this.cartData[params.siteIndex].cartList[params.cartIndex].source_sku_id,
								num: this.cartData[params.siteIndex].cartList[params.cartIndex].num
							}
							this.inventoryConFun(onlyObj);
							this.calculationTotalPrice(true);
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			},
			/**
			 * 结算
			 */
			settlement(e) {
				if (this.totalCount > 0) {
					let cart_ids = [];
					let Instead_array = [];
					let ishaveJDgoods=false;
					
					this.cartData.forEach(siteItem => {
						siteItem.cartList.forEach(item => {
							if (item.checked) {
								cart_ids.push(item.cart_id);
								console.log(item,'item');
								if(item.source == 2){
									ishaveJDgoods=true;
								}
							}
						});
					});
					
					// 判断是否有四级地址
					if(ishaveJDgoods == true && this.locationObj.community_id == 0){
						uni.showModal({
							title: '温馨提示',
							content: '购买该商品需要填写街道，是否前往编辑？',
							success: (res) => {
								if (res.confirm) {
									this.$util.redirectTo('/otherpages/member/address/address',{'back': 'pages/goods/cart/cart'});
								}
							}
						}); 
						return false
					}

					if (this.isSub) return;
					this.isSub = true;

					if (e == 1) {
						// 正常下单
						uni.setStorage({
							key: 'orderCreateData',
							data: {
								cart_ids: cart_ids.toString()
							},
							success: () => {
								this.$util.redirectTo('/pages/order/payment/payment',{address:this.locationObj.full_address});
								this.isSub = false;
							}
						});
					} else {
						// 代客下单
						this.isSub = false;
						this.GetCodeImg(cart_ids)
					}
				}
			},
			//获取二维码
			GetCodeImg(cart_ids) {
				let path = config.h5Domain + '/pages/index/index/index' + '?action=payment_from_shop&cart_ids=' + cart_ids.toString();
				this.$api.sendRequest({
					url: '/api/order/replaceOrderQrcode',
					data: {
						path: path,
					},
					success: res => {
						if (res.code >= 0) {
							this.CodeImg = res.data.path + '?time=' + (new Date()).getTime(); 
							this.$refs.Instead_popup.open()
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			},
			close_popup() {
				this.$refs.Instead_popup.close()
			},
			save_img() {
				uni.downloadFile({
					url: this.$util.img(this.CodeImg),
					success: (res) => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.showToast({
										title: "保存成功",
										icon: "success"
									});
								},
								fail: function() {
									uni.showToast({
										title: "保存失败，请稍后重试",
										icon: "none"
									});
								}
							});
						}
					}
				})
			},
			edit(index) {
				this.cartData[index].edit = !this.cartData[index].edit;
			},
			/**
			 * 清空失效商品
			 */
			clearInvalidGoods() {
				var cart_ids = [];
				this.invalidGoods.forEach(goodsItem => {
					cart_ids.push(goodsItem.cart_id);
				});
				if (cart_ids.length) {
					this.$api.sendRequest({
						url: '/api/cart/delete',
						data: {
							cart_id: cart_ids.toString()
						},
						success: res => {
							if (res.code >= 0) {
								this.invalidGoods = [];
								this.getCartNumber()
							} else {
								this.$util.showToast({
									title: res.message
								});
							}
						}
					});
				}
			},
			imageError(siteIndex, cartIndex) {
				this.cartData[siteIndex].cartList[cartIndex].sku_image = this.$util.getDefaultImage().default_goods_img;
				this.$forceUpdate();
			},
			// 购物车数量
			getCartNumber() {
				if (uni.getStorageSync("token")) {
					this.$store.dispatch('index/getCartNumber')
				}
			},
			//获取默认收货地址
			getDefaultAddress(){
				let that = this;
				this.$api.sendRequest({
					url: '/api/memberaddress/default',
					data: {},
					success: res => {
						// console.log('获取默认收货地址',res)
						if(res.code == 0 && res.data){
							this.defaultAddress = res.data;
						}
					},
				});
			},
			
			// -------------------------------------复制粘贴拿取城市选择四级联动--------------------------------------------
			// 拿城市列表
			popListFun(){
				this.popConSta = 0;
				this.$api.sendRequest({
					url: '/api/address/lists',
					success: res => {
						// console.log(res,'列表')
						if (res.code >= 0) {
							this.popList = res.data;
							this.listAr[this.popConSta] = res.data;
							this.popConSta += 1;
						}
					}
				});
			},
			
			// 点击获取下一级
			popBtn(val,id){
				let sta = this.popCon.length;
				let posData = {};
				let urlCon = '';

				
				let obj = {
					name:val,
					id:id
				}
				
				this.bottomSta = 4;
			
				if(this.popConSta == 1){
					this.popIdA = id;
					urlCon = '/api/address/lists';
					posData = {
						'pid' : this.popIdA
					}
					let arr = this.popCon;
					arr.splice(1);
					this.popCon = arr;
					this.popCon[0] = obj;
				}else if(this.popConSta == 2){
					this.popIdB = id;
					urlCon = '/api/address/lists';
					posData = {
						'pid' : this.popIdB,
						// 'series_name_id' : this.popIdB
					}
					let arr = this.popCon;
					arr.splice(2);
					this.popCon = arr;
					this.popCon[1] = obj;
				}else if(this.popConSta == 3){
					this.popIdC = id;
					urlCon = '/api/address/lists';
					posData = {
						'pid' : this.popIdC,
						// 'series_name_id' : this.popIdB,
						// 'model_year': this.popIdC
					}
					let arr = this.popCon;
					arr.splice(3);
					this.popCon = arr;
					this.popCon[2] = obj;
				}else if(this.popConSta == 4){
					this.popIdD = id;
					this.popCon[3] = obj;
					// console.log(this.popIdA,this.popIdB,this.popIdC,this.popIdD,'最后');
					this.popShow = false;
					this.vehicleModel = this.popCon[0].name + '-' + this.popCon[1].name + '-' + this.popCon[2].name + '-' + this.popCon[3].name; 
					this.bottomSta = 3;
					let siteId = {
						province_id: this.popCon[0].id,
						city_id: this.popCon[1].id,
						district_id: this.popCon[2].id,
						community_id: this.popCon[3].id,
						full_address: this.vehicleModel
					}
					this.locationObj = siteId;
					this.inventoryConFun();
					return;
				}else{
					this.popIdD = id;
					this.popCon[3] = obj;
					// console.log(val,id,'最后');
					this.popShow = false;
					this.vehicleModel = this.popCon[0].name + '-' + this.popCon[1].name + '-' + this.popCon[2].name + '-' + this.popCon[3].name;
					this.bottomSta = 3;
					let siteId = {
						province_id: this.popCon[0].id,
						city_id: this.popCon[1].id,
						district_id: this.popCon[2].id,
						community_id: this.popCon[3].id,
						full_address: this.vehicleModel
					}
					this.locationObj = siteId;
					this.inventoryConFun();
					return;
				}
				let that = this;
				this.$api.sendRequest({
					url: urlCon,
					data: posData,
					success: res => {
						// console.log(res,'拿取下一级')
						if (res.code >= 0) {
							this.popList = res.data;
							this.listAr[this.popConSta] = res.data;
							this.popConSta += 1;
						}else{
							uni.showToast({
								title:res.message,
								icon:'none'
							})
						}
					}
				});
			},
			
			
			// 选择某一项
			popConBtn(con,e){
				this.bottomSta = e;
				// console.log(con,e)
				if(e < 4){
					this.popConSta = e+1;
					if(e == 0){
						this.popListFun();
					}else{
						this.popList = this.listAr[e];
					}
				}else{
					this.popList = this.listAr[this.listAr.length-1];
				}
				
			},
			
			// 默认地址
			getAddress(id){
				this.$api.sendRequest({
					url: '/api/memberaddress/setdefault',
					data: {
						id:id
					},
					success: res => {
						if (res.data > 0) {
				// 			this.$refs.mescroll.refresh();
				// 			if (this.back != '') {
				// 				let jump = true;
				// 				let arr = getCurrentPages().reverse();
				// 				console.log(arr.length)
				// 				for (let i = 0; i < arr.length; i++) {
				// 					if (this.back.indexOf(arr[i].route) != -1) {
				// 						jump = false;
				// 						uni.navigateBack({
				// 							delta: i
				// 						});
				// 						break;
				// 					}
				// 				}
				// 				return false;
				// 				if (jump) {
				// 					this.$refs.mescroll.refresh();
				// 					this.$util.showToast({
				// 						title: '修改默认地址成功'
				// 					});
				// 					setTimeout(()=>{
				// 						this.$util.redirectTo(this.back, {address:item.full_address}, 'redirectTo');
				// 					},1000)
									
				// 				}
				
				// 			}
						} else {
						}
					}
				});
			},
			
			// 查询当前地址是否有库存
			inventoryConFun(checkSta){
				let that = this;
				let param = [];
				this.modifyFlag = true;
				if(checkSta){
					if(checkSta.source == 2){
						let obj = {
							skuId: checkSta.source_sku_id,
							num: checkSta.num
						}
						param.push(obj)
					}
				}else{
					if(that.checkedStaCon.length){
						that.checkedStaCon.forEach(siteItem => {
							if(siteItem.source == 2){
								let obj = {
									skuId: siteItem.source_sku_id,
									num: siteItem.num
								}
								param.push(obj);
							}
						});
					}
				}

				this.$api.sendRequest({
					url: '/jingdong/api/jdserver/getNewStockBySkuId',
					data: {
						skuparams: JSON.stringify(param),
						province_id: that.locationObj.province_id,
						city_id: that.locationObj.city_id,
						district_id: that.locationObj.district_id,
						community_id: that.locationObj.community_id
					},
					success: res => {
						// console.log('打印库存信息',res)
						if(res.code == 0){
							// this.inventorySta = res.data[0].msg;
							// console.log('库存里面获取购物车',that.cartData);
							
							for(let i=0; i<that.cartData.length; i++){
								for(let j=0; j<that.cartData[i].cartList.length; j++){
									for(let k=0; k<res.data.length; k++){
										if(that.cartData[i].cartList[j].source_sku_id == res.data[k].skuId){
											if(res.data[k].resultCode != 1){
												that.cartData[i].cartList[j].repertorySta = res.data[k].msg;
												that.cartData[i].cartList[j].checked = false;
												that.cartData[i].cartList[j].repertoryChecked = 'ban';
											}else{
												that.cartData[i].cartList[j].repertorySta = '';
												// that.cartData[i].cartList[j].checked = true;
												that.cartData[i].cartList[j].repertoryChecked = '';
											}
										}
									}
								}
							}
							this.calculationTotalPrice(true);
							that.$forceUpdate();
							// console.log('购物车信息：',that.cartData);
						}
					},
					fail: res => {
				
					},

				});

			},
			
			// 定位拿取地址经纬度
			locationConFun(){
				console.log('开始调用定位函数')
				let that = this;
				uni.getLocation({
				    type: 'gcj02',
				    success: function (data) {
						let lng = (data.longitude).toFixed(6);
						let lat = (data.latitude).toFixed(6);
						console.log(data,'定位成功获取')
						
						that.$api.sendRequest({
							url: '/jingdong/api/jdserver/getAddressByLngLat',
							data: {
								lng: lng,
								lat: lat
							},
							success(res){
								console.log(res,'定位接口请求完成');
								if(res.code == 0){
									
									
									that.locationObj = res.data;
									// that.vehicleModel = res.data.full_address;
									
									// let siteId = {
									// 	province_id: res.data.province_id,
									// 	city_id: res.data.city_id,
									// 	district_id: res.data.district_id,
									// 	community_id: res.data.community_id
									// }
									
									// that.inventoryConFun();
									
								}
							},
							fail(err){
						
							}
						});
				    }
				});
			},
			
			// 哪些商品需要获取库存
			checkedStaFun(){
				this.modifyFlag = true;
				if (this.cartData.length) {
					this.checkedStaCon = [];
					let arr = [];
					this.cartData.forEach(siteItem => {
						siteItem.cartList.forEach(item => {
							
							// 获取选中状态的
							if (item.checked) {
								let obj = {
									num: item.num,
									source: item.source,
									source_sku_id: item.source_sku_id
								}
								arr.push(obj);
							}
							
							
						});
					});
					this.checkedStaCon = arr;
					this.inventoryConFun();
				} else {
					console.log('空')
				}
			}
		}
	};
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100%;
		padding-bottom: 55px;
		padding-bottom: calc(55px + constant(safe-area-inset-bottom))constant(safe-area-inset-bottom);
		padding-bottom: calc(55px + env(safe-area-inset-bottom));
		padding-top: 20rpx;
	}
	.top_siteClass{
		width: 95%;
		height: 60rpx;
		background-color: #eee;
		color: #646464;
		text-align: center;
		line-height: 60rpx;
		border-radius: 30rpx;
		margin: 0 auto;
		font-size: 24rpx;
	}

	.blockView {
		width: 100%;
		height: 100rpx;
	}

	.Instead_popup {
		.Instead-title {
			padding: 30rpx 0;
			text-align: center;
		}

		.Instead-box {
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				background-color: #F8F8F8;
				width: 350rpx;
				height: 350rpx;
			}
		}

		.Instead-footer {
			display: flex;
			justify-content: space-around;
			align-items: center;
			padding-top: 60rpx;
			padding-bottom: 30rpx;

			button {
				border-radius: 10rpx;
				width: 46%;
				margin: 0;
			}

			.close-btn {
				background-color: #999999;
				color: white;
			}

			.sue-btn {
				background-color: #34C25D;
				color: white;
			}
		}
	}

	.cart-wrap {
		margin: 20rpx;
		background: #fff;
		border-radius: $ns-border-radius;
		overflow: hidden;
		width: calc(100% - 20px);

		.cart-header {
			padding: 20rpx;
			overflow: hidden;
			display: flex;
			align-items: center;
			line-height: 40rpx;

			.shop-info {
				flex: 1;
				line-height: inherit;
			}

			.iconyuan_checkbox {
				font-size: 36rpx;
				color: #898989;
				margin-right: 14rpx;
				line-height: 1;
			}

			.iconyuan_checked {
				font-size: 36rpx;
				margin-right: 14rpx;
				line-height: 1;
			}

			.icondianpu {
				display: inline-block;
				margin-right: 10rpx;
				line-height: inherit;
			}

			.cart-operation {
				line-height: inherit;
			}
		}

		.cart-goods {
			margin: 0 20rpx 20rpx 20rpx;
			border-radius: 4px;
			background: #fff;
			box-sizing: border-box;
			position: relative;

			.goods-wrap {
				display: flex;
				position: relative;
				padding-left: 52rpx;
				transition: all 0.3s;

				&.edit {
					transform: translateX(-120rpx);
				}

				&>.iconfont {
					font-size: 36rpx;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
				}

				&>.iconyuan_checkbox {
					color: $ns-text-color-gray;
				}

				.goods-img {
					width: 180rpx;
					height: 180rpx;
					padding: 20rpx 0 0 0;
					margin-right: 20rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.goods-info {
					flex: 1;
					position: relative;
					padding: 20rpx 0 0 0;
					max-width: calc(100% - 200rpx);

					.goods-name {
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						line-height: 1.5;
						font-size: 28rpx;
					}

					.sku {
						font-size: 24rpx;
						color: #999;
						display: block;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.goods-sub-section {
						position: absolute;
						left: 0;
						bottom: 0;
						width: 100%;
						display: flex;
						flex-direction: row;
						align-items: flex-end;
						justify-content: space-between;

						.goods-price-box {
							display: flex;
							flex-direction: column;

							.goods-price {
								line-height: 36rpx;
								font-size: 28rpx;

								.tag {
									border: 1px solid $base-color;
									font-size: 24rpx;
									padding: 2rpx 4rpx;
									border-radius: 4rpx;
								}

								.unit {
									font-size: 28rpx;
									padding-right: 6rpx;
								}

							}

							.tag {
								padding-bottom: 2rpx;
							}
						}


						.uni-numbox {
							float: right;
						}
					}
				}
			}

			.item-del {
				position: absolute;
				width: 0;
				height: 100%;
				color: #fff;
				right: 0;
				top: 0;
				display: flex;
				align-items: center;
				justify-content: center;
				transition: all 0.3s;
				overflow: hidden;
				white-space: nowrap;

				&.show {
					width: 100rpx;
				}
			}
		}

		.invalid-goods {
			.invalid-mark {
				background: #aaa;
				color: #fff;
				padding: 4rpx 16rpx;
				display: inline-block;
				line-height: 1;
				font-size: 24rpx;
				position: absolute;
				border-radius: 24rpx;
				top: 50%;
				transform: translateY(-50%);
			}

			.goods-wrap {
				padding-left: 100rpx;
			}
		}
	}

	.cart-bottom {
		position: fixed;
		z-index: 5;
		width: 100vw;
		height: 122rpx;
		background: #fff;
		bottom: var(--window-bottom);
		overflow: hidden;
		display: flex;
		bottom: 56px;
		bottom: calc(56px + constant(safe-area-inset-bottom));
		bottom: calc(56px + env(safe-area-inset-bottom));

		.all-election {
			height: 122rpx;
			position: relative;
			// padding-left: 20rpx;
			display: inline-block;

			&>.iconfont {
				font-size: 36rpx;
				position: absolute;
				top: 50%;
				left: 24rpx;
				transform: translateY(-50%);
			}

			&>text {
				margin-left: 56rpx;
				line-height: 122rpx;
			}
		}

		.settlement-info {
			flex: 1;
			text-align: right;
			// padding-right: 20rpx;
			line-height: 122rpx;
			display: flex;
			align-items: center;
			// display: flex;
			// justify-content: space-between;
			// .right{
			// 	width: 30%;
			// 	text-align: center;
			// }
			// .left{
			// 	width: 70%;
			// 	display: flex;
			// 	flex-direction: column;
			// 	justify-content: space-around;
			// 	line-height: 1.2;
			// 	align-items: flex-start;

			// }
			/* .left{
				min-width:68rpx !important;
				padding-right:4rpx;
			} */
			.right {
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				padding-left: 20rpx;
				padding-top: 10rpx;

				.count-line-title {
					line-height: 26rpx;
					text-align: left;

					text {
						color: $base-color;
						padding-right: 6rpx;
					}
				}

				.count-line {
					// margin-left: 66rpx;
					line-height: 26rpx;
					height: 76rpx;
					text-align: left;

					// display: flex;
					// flex-direction: column;
					// align-items: flex-start;
					// justify-content: space-around;
				}

				.top {
					.total {
						color: #000;
					}

					text {
						font-size: 28rpx;
					}
				}

				.bottom {
					padding-top: 4rpx;

					text {
						font-size: 20rpx;
						color: #909399;
					}
				}

			}
		}

		.operation-btn {
			width: 350rpx;
			height: 122rpx;
			line-height: 122rpx;
			border-radius: 0;
			margin: 0;
			display: flex;
			justify-content: flex-end;
			align-items: center;

			button {
				margin: 0 10rpx !important;
				padding: 0 20rpx;
				height: 65rpx;
				line-height: 65rpx;
			}

			button:last-child {
				margin-right: 20rpx !important;
			}

			.btn-color {
				background-color: #1791FD;
				color: white;
			}
		}
	}

	.cart-empty {
		text-align: center;
		font-size: 24rpx;
		padding: 240rpx $ns-padding 80rpx $ns-padding;

		.empty {
			padding-top: 0;
			padding-bottom: 50rpx;
			text-align: center;
		}

		image {
			margin: 10px auto;
			width: 220rpx;
			height: 120rpx;
			display: block;
		}

		navigator {
			display: inline;
		}
	}
	.lch_pop{
		width: 100%;
		height: 100%;
	}
	.lch_pop_tit{
		font-size: 30rpx;
		font-weight: bold;
		height: 8.5%;
		display: flex;
		align-items: center;
		justify-content: center;
		border-bottom: 2rpx solid #eee;
	}
	.lch_pop_conwrap{
		min-width: 100%;
		height: 11.2%;
		border-bottom: 2rpx solid #eee;
		display: flex;
		justify-content: left;
		padding: 0 30rpx 4rpx;
		overflow-y: hidden;
		overflow-x: scroll;
	}
	.lch_pop_con{
		height:100%;
		display: flex;
		align-items: center;
		// background-color: red;
		font-size: 28rpx;
		color: #333333;
		margin-right: 30rpx;
		white-space:nowrap;
		
	}
	.lch_pop_List{
		width: 100vw;
		height: 80%;
		// background-color: pink;
		overflow: hidden;
		overflow-y: auto;
	}
	.lch_pop_List_min{
		width: 100vw;
		height: 120rpx;
		border-bottom: 2rpx solid #eee;
		line-height: 120rpx;
		padding-left: 30rpx;
		font-size: 28rpx;
		color: #333333;
	}
	
	.lch_pop_ListA{
		width: 100vw;
		height: 78%;
		// background-color: pink;
		overflow: hidden;
		overflow-y: auto;
	}
	.lch_pop_ListA_btn{
		margin: 30rpx auto 0;
		width: 90%;
		height: 80rpx;
		border-radius: 40rpx;
		background-color: #FF4948;
		color: #FFFFFF;
		text-align: center;
		line-height: 80rpx;
	}
</style>
<style scoped>
	/deep/ .uni-popup__wrapper-box {
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
	}

	/deep/ .uni-popup__wrapper.uni-bottom {
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
	}
</style>
